package com.nondo.dean.platform.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Area;
import com.nondo.dean.platform.model.AreaBean;
import com.nondo.dean.platform.service.AreaService;

/**
 * 区域管理后台接口
 * @author zlj
 * @date 2018年4月3日
 */
@RestController("apiAdminArea")
@RequestMapping("/{v}/admin/area")
public class AreaController {
    
    @Autowired
    AreaService areaService;
    
    /**
     * 查看区域详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<Area> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<Area> view = new View<Area>(20000, "获取数据成功");
        
        Area area = null;
        if (id != null) {
            area = areaService.selectArea(id);
        } else {
            area = new Area();
        }

        view.setData(area);
        return view;
    }
    
    /**
     * 添加区域
     * @param request
     * @param response
     * @param area 区域信息 {@link Area}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Area> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody Area area, BindingResult result) {
        View<Area> view = new View<Area>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Area areaDb = this.areaService.selectAreaByName(area.getName());
            if (areaDb != null) {
                return View.of500Error("区域名称已存在");
            }
            this.areaService.addArea(area);
        }
        return view;
    }

    /**
     * 更新区域
     * @param request
     * @param response
     * @param area 区域信息 {@link Area}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Area> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody Area area, BindingResult result) {
        View<Area> view = new View<Area>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Area areaDb = areaService.selectArea(area.getId());
            if (areaDb.getName().equals(area.getName()) && areaDb.getId() != area.getId()) {
                return View.of500Error("区域名称已存在");
            }
            BeanUtils.copy(area, areaDb);
            this.areaService.updateArea(areaDb);
        }

        return view;
    }

    /**
     * 获取区域分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Area>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Area> query) {
        View<Page<Area>> view = new View<Page<Area>>(20000, "获取列表成功");

        Area area = query.getData();

        Page<Area> page = new Page<Area>(query.getPage(), query.getLimit(), areaService.count(area));
        
        List<Area> areaList = areaService.getAreaList(query.getOffset(), query.getLimit(), area);
        
        page.setContent(areaList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除区域
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        Area area = areaService.selectArea(id);
        area.setDelFlag(String.valueOf(1));
        this.areaService.updateArea(area);

        return view;
    }
    
    /**
     * 获取区域列表
     * @param request
     * @param response
     * @param code 城市代码
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list/{code}")
    public View<List<AreaBean>> list(HttpServletRequest request, HttpServletResponse response, @PathVariable("code") String code) {
        View<List<AreaBean>> view = new View<List<AreaBean>>(20000, "获取列表成功");

        List<AreaBean> areaList = areaService.getAreaListByCityCode(code);
        
        view.setData(areaList);
        
        return view;
    }
}